package com.iafme.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iafme.dto.req.CourseReqDTO;
import com.iafme.dto.res.CourserResDTO;
import com.iafme.model.Course;
import com.iafme.service.CourseService;
import com.systemframework.annotation.OperateAnnotation;
import com.systemframework.common.BaseCommonInterface;
import com.systemframework.enums.BusinessType;
import com.systemframework.util.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author summer
 * @version 1.0
 * @date 2025/1/7 18:05
 */
@RestController
@RequestMapping(value = "/course")
@AllArgsConstructor
public class CourseController implements BaseCommonInterface<CourseReqDTO, CourserResDTO> {

    private final CourseService courseService;

    @OperateAnnotation(businessType = BusinessType.INSERT,name = "考试信息")
    @SaCheckPermission("enter:course:add")
    @Override
    public Result<String> save(@Valid CourseReqDTO courseReqDTO) {
        Course course = Convert.convert(Course.class, courseReqDTO);
        courseService.save(course);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.UPDATE,name = "考试信息")
    @SaCheckPermission("enter:course:edit")
    @Override
    public Result<String> update(@Valid CourseReqDTO courseReqDTO) {
        Course course = Convert.convert(Course.class, courseReqDTO);
        courseService.updateById(course);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.DELETE,name = "考试信息")
    @SaCheckPermission("enter:course:delete")
    @Override
    public Result<String> del(Integer id) {
        courseService.removeById(id);
        return Result.successResult();
    }

    @Override
    public Result<Page<CourserResDTO>> lists(CourseReqDTO courseReqDTO) {
        return Result.successResult(courseService.lists(courseReqDTO));
    }

    @Override
    public Result<CourserResDTO> findById(Integer id) {
        return Result.successResult(Convert.convert(CourserResDTO.class,courseService.getById(id)));
    }

    @OperateAnnotation(businessType = BusinessType.BATCHDELETE,name = "考试信息")
    @SaCheckPermission("enter:course:delete")
    @Override
    public Result<String> batchDel(List<CourserResDTO> list) {
        List<Integer> ids = list.stream().map(CourserResDTO::getId).collect(Collectors.toList());
        courseService.removeByIds(ids);
        return Result.successResult();
    }
}
